﻿using SteamWebAPI;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Stack_Check
{
    class PlayerListItem : Label
    {
        private SteamUser user;

        private BitmapImage image;


        public SteamUser User
        {
            get { return this.user; }
        }

        public static Brush[] Colors = {Brushes.Black, Brushes.Red, Brushes.Blue, Brushes.Green, Brushes.Gold, Brushes.Gray, Brushes.Indigo };

        public PlayerListItem(SteamWebAPI.SteamUser user)
        {
            this.FontSize = 16;
            
            this.user = user;
            if (user.AccountVisibilityLevel != 3)
            {
               // this.Content = "○× " + user.Name;
                this.ToolTip = "Nelze zjistit seznam přátel";
            }
            else
            {
               // this.Content = user.Name;
            }

            if (user.Overview != null && user.Overview.Status == 15)
            {
                this.ToolTip += Environment.NewLine + user.Overview.StatusDetail;
            }

            string uri = "http://cdn.steamcommunity.com/public/images/countryflags/";
            uri += user.CountryTag.ToLower();
            uri += ".gif";
            Uri flagUri = new Uri(uri);
            BitmapImage flag = new BitmapImage();
            image = new BitmapImage(flagUri);
            
            this.Height = 35;
            this.Width = 225;

            Foreground = PlayerListItem.Colors[user.Group];
            
        }

        protected override void OnRender(DrawingContext drawingContext)
        {

            FormattedText formatedText = new FormattedText(user.Name, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface("Segoe UI"), 16, PlayerListItem.Colors[user.Group]);

            base.OnRender(drawingContext);
            //16x11
            drawingContext.DrawImage(this.image, new System.Windows.Rect(this.Width - 18, 3, 16, 11));

            if (user.AccountVisibilityLevel != 3)
            {
                using (MemoryStream memory = new MemoryStream())
                {
                    Stack_Check.Properties.Resources.steam_icon.Save(memory, ImageFormat.Png);
                    memory.Position = 0;
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.BeginInit();
                    bitmapImage.StreamSource = memory;
                    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                    bitmapImage.EndInit();
                    drawingContext.DrawImage(bitmapImage, new System.Windows.Rect(5, 7, 10, 10));
                    
                }
            }
            if (user.Overview != null && user.Overview.Status == 15)
            {
                using (MemoryStream memory2 = new MemoryStream())
                {
                    Stack_Check.Properties.Resources.db_icon.Save(memory2, ImageFormat.Png);
                    memory2.Position = 0;
                    BitmapImage bitmapImage2 = new BitmapImage();
                    bitmapImage2.BeginInit();
                    bitmapImage2.StreamSource = memory2;
                    bitmapImage2.CacheOption = BitmapCacheOption.OnLoad;
                    bitmapImage2.EndInit();
                    drawingContext.DrawImage(bitmapImage2, new System.Windows.Rect(5, 18, 10, 10));
                }
            }


            drawingContext.DrawText(formatedText, new System.Windows.Point(20, 5));

            string label = user.Info.Winrate.ToString("P2");
            Brush foreground;
            if (user.Info.Winrate > 0.5)
            {
                foreground = Brushes.Green;
            }
            else
            {
                foreground = Brushes.Red;
            }

            FormattedText winrateFormat = new FormattedText(label, System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface("Segoe UI"), 10, foreground);
            drawingContext.DrawText(winrateFormat, new System.Windows.Point(this.Width - 18, 17));
        }
        
    }
}
